package net.melon.agency.inst.ssh.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.esj.auth.pojo.CoreAdmin;
import net.esj.basic.core.engine.config.MelonConfig;
import net.esj.basic.core.engine.util.enumeration.EnumProvidersFactory;
import net.esj.basic.pojo.EnumStructure;
import net.esj.basic.utils.StringUtils;
import net.esj.basic.utils.view.BaseJsonMessage.MessageLevel;
import net.esj.basic.view.AbstractPluginPageAction;
import net.melon.agency.inst.ssh.service.AuthService;
import net.melon.agency.inst.ssh.service.UserService;
import net.melon.agency.pojo.Agency;
import net.melon.agency.pojo.AgencyGroup;
import net.melon.agency.pojo.AgencyUser;
import net.melon.agency.pojo.AgencyUser.AgencyUserState;

public class UserAction extends AbstractPluginPageAction<AgencyUser, AgencyUser> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2400314125216577065L;

	@Resource
	private UserService userService;
	
	@Resource
	private EnumProvidersFactory enumProvidersFactory;
	
	@Resource
	private AuthService authService;
	
	private int state;
	
	private boolean flag;
	
	private String[] groups;
	
	private int u;
	
	private String name;
	
	private String agencyName;
	
	public void state(){
		userService.changeUserState(id, state);
		writeBaseJsonMessage("", getText(ACTION_SUCCESS), MessageLevel.INFO, "html");
	}
	
	@Override
	protected void doQueryPage() {
		/*if(getOperator().getMType().equals(AgencyUser.MOBJECT_TYPE)){
			AgencyUser user = getSessionUser(AgencyUser.class);
			if(user!=null){
				List<String> list = new ArrayList<String>();
				list.add(user.getId());
				list = authService.getChildAgencyUser(user.getId(),list);
				pagination.getQueryBuilder().in("id", list).fetch("agency");
				pagination = userService.pageUser(pagination);
			}
		}*/
		
		if(StringUtils.hasText(id)){
			Agency agency  = null;
			agency= new Agency(id);

			pagination.getQueryBuilder().fetch("agency");
			pagination = userService.pageUser(pagination, agency);
		}else{
			//if(getOperator().getMType().equals(AgencyUser.MOBJECT_TYPE)){
				AgencyUser user = getSessionUser(AgencyUser.class);
				if(user!=null){
					List<String> list = new ArrayList<String>();
					list.add(user.getId());
					list = authService.getChildAgencyUser(user.getId(),list);	
					
					if(agencyName!=null&&(agencyName.length()>0)){
						pagination.getQueryBuilder().in("id", list).fetch("agency").like("agency.name", "%"+agencyName+"%");
					}else{
						pagination.getQueryBuilder().in("id", list).fetch("agency");
					}
					pagination = userService.pageUser(pagination);
				}
			//}
		}
		
	}
	
	@Override
	protected void doAdd() throws Exception {
		if(getOperator().getMType().equals(AgencyUser.MOBJECT_TYPE)){
			AgencyUser user = getSessionUser(AgencyUser.class);
			if(user!=null){
				entity.setAgencyUseId(user.getId());
			}
		}else{
			CoreAdmin user = getSessionUser(CoreAdmin.class);
			if(user!=null){
				entity.setAgencyUseId(user.getId());
			}
			groups = new String[]{MelonConfig.getProperty(AgencyGroup.AGENCY_GROUP_DEFAULT)};
		}
		userService.saveUser(entity,getOperator(),groups);
	}
	//复制权限
	public void copyAuth(){
		AgencyUser au = authService.getAgencyUserWithName(name,id);
		if(au==null){
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), "复制权限账号不存在！", MessageLevel.INFO, "html");
		}else{
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), addSuccMsg(), MessageLevel.INFO, "html");
		}
	}
	@Override
	protected void doLoad() {
		entity = userService.getUser(id,true);
	}

	public void currentUser(){
		entity = getSessionUser(AgencyUser.class);
		entity = userService.getUser(entity.getId(), false);
		writeJson(entity);
	}
	
	@Override
	protected void doDelete() throws Exception {
		userService.deleteUser(id);
	}

	@Override
	protected void doGetEntity() {
		if(getNamespace().indexOf("agency")>0){
			AgencyUser au = getSessionUser(AgencyUser.class);
			if(au.getId().equals(id)){
				u = 1;
			}
		}
		entity = userService.getUser(id,true);
	}

	@Override
	protected void doUpdate() throws Exception {
		entity = userService.updateUser(entity,getOperator(),groups);
	}

	public void setEntity(AgencyUser entity){
		this.entity = entity;
	}
	
	public AgencyUser getEntity(){
		return this.entity;
	}
	

	public int getState() {
		return state;
	}


	public void setState(int state) {
		this.state = state;
	}
	
	public AgencyUserState[] getStates(){
		return AgencyUserState.values();
	}
	
	public Set<AgencyGroup> getGroups() {
		if(getOperator().getMType().equals(CoreAdmin.MOBJECT_TYPE)){
			return Collections.EMPTY_SET;
		}else{
			AgencyUser user = getSessionUser(AgencyUser.class);
			if(user!=null){
				return user.getGroups();
			}
			return Collections.EMPTY_SET;
		}
	}


	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public void setGroups(String[] groups) {
		this.groups = groups;
	}
	
	public int getU() {
		return u;
	}

	public void setU(int u) {
		this.u = u;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAgencyName() {
		return agencyName;
	}

	public void setAgencyName(String agencyName) {
		this.agencyName = agencyName;
	}

}
